package com.assistant.framework;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * 处理分页数据
 * <p>优先级为 用户手动设置 > Http请求中的参数 > 默认</p>
 * 使用手动设置的， 使用完后需要调用reset进行清理
 * <ul>
 * Http请求中的参数 支持两种类型
 * <li>1. page和size组合 page: 当前页 从1开始, size: 每页记录数</li>
 * <li>2. start和limit组合 start: 开始记录数, limit: 每页记录数</li>
 * </ul>
 * 默认 从第一页开始， 每页记录数为20
 */
public class PageUtils {

    private static final ThreadLocal<Integer> page = new ThreadLocal<>();
    private static final ThreadLocal<Integer> size = new ThreadLocal<>();

    public static void set(Integer page, Integer size) {
        PageUtils.page.set(page);
        PageUtils.size.set(size);
    }

    public static void reset() {
        PageUtils.page.remove();
        PageUtils.size.remove();
    }

    public static PageRequest currentPage() {
        // 优先级从 threadLocal > request > default
        PageRequest pageRequest;
        if((pageRequest = buildFromThreadLocal()) != null) {
            return pageRequest;
        } else if((pageRequest = buildFromRequest()) != null) {
            return pageRequest;
        } else {
            return defaultPage();
        }
    }

    private static PageRequest defaultPage() {
        return PageRequest.of(0, 15);
    }

    private static PageRequest buildFromRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        String pageParameter = servletRequestAttributes.getRequest().getParameter("page");
        String sizeParameter = servletRequestAttributes.getRequest().getParameter("size");
        if (pageParameter != null && sizeParameter != null)
            return PageRequest.of(Integer.parseInt(pageParameter) - 1, Integer.parseInt(sizeParameter));

        String startParameter = servletRequestAttributes.getRequest().getParameter("start");
        String limitParameter = servletRequestAttributes.getRequest().getParameter("limit");
        if (startParameter != null && limitParameter != null)
            return PageRequest.of(Integer.parseInt(startParameter) / Integer.parseInt(limitParameter), Integer.parseInt(limitParameter));
        return null;
    }

    public static <T> List<T> buildToResponse(PageResult<T> pageResult) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (servletRequestAttributes.getResponse() != null) {
            servletRequestAttributes.getResponse().setHeader("total", String.valueOf(pageResult.getTotalCount()));
        }
        return pageResult.getItems();
    }

    public static <T> List<T> buildToResponse(Page<T> pageResult) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        if (servletRequestAttributes.getResponse() != null) {
            servletRequestAttributes.getResponse().setHeader("total", String.valueOf(pageResult.getTotalElements()));
        }
        return pageResult.getContent();
    }

    private static PageRequest buildFromThreadLocal() {
        Integer page = PageUtils.page.get();
        Integer size = PageUtils.size.get();
        return (page != null && size != null) ? PageRequest.of(page, size) : null;
    }

    // 是否是一个分页请求
    public static boolean isPageRequest() {
        return buildFromRequest() != null;
    }
}
